package com.jt.springbootdemomybatis3;

import com.jt.springbootdemomybatis3.mapper.DemoUserMapper;
import com.jt.springbootdemomybatis3.pojo.DemoUser;
import org.apache.catalina.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class demo01 {
    @Test
    public void testDemo1() throws IOException {

        /*创建SqlSessionFactory*/
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        /*从SqlSessionFactory中获取sqlSession*/
        SqlSession sqlSession = sqlSessionFactory.openSession();

        /*获取mapper接口,执行接口方法*/
        DemoUserMapper userMapper = sqlSession.getMapper(DemoUserMapper.class);
        List<DemoUser> userList = userMapper.findAll();
        System.out.println(userList);
        sqlSession.close();
    }
    @Test
    public void testFindOne() throws IOException {

        /*创建SqlSessionFactory*/
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        /*从SqlSessionFactory中获取sqlSession*/
        SqlSession sqlSession = sqlSessionFactory.openSession();

        /*获取mapper接口,执行接口方法*/
        DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
       int id=1;
        DemoUser one = demoUserMapper.findOne(id);
        System.out.println(one);
        sqlSession.close();
    }

    @Test
    public void testFindTwo() throws IOException {

        /*创建SqlSessionFactory*/
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        /*从SqlSessionFactory中获取sqlSession*/
        SqlSession sqlSession = sqlSessionFactory.openSession();

        /*获取mapper接口,执行接口方法*/
        DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
        String name="王昭君";
//        DemoUser one = demoUserMapper.findTwo(name);
        List<DemoUser> list= demoUserMapper.findTwo(name);
        System.out.println(list);
        sqlSession.close();
    }

    @Test
    public void testFindThree() throws IOException {

        /*创建SqlSessionFactory*/
        String resource = "mybatis/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        /*从SqlSessionFactory中获取sqlSession*/
        SqlSession sqlSession = sqlSessionFactory.openSession();

        /*获取mapper接口,执行接口方法*/
        DemoUserMapper demoUserMapper = sqlSession.getMapper(DemoUserMapper.class);
      String sex="女";
      int age=18;
//        DemoUser one = demoUserMapper.findThree(sex);
        List<DemoUser> list= demoUserMapper.findThree(sex,age);
        System.out.println(list);
        sqlSession.close();
    }
}
